import pymongo
import requests
import os
import re
client = pymongo.MongoClient(host='localhost', port=27017)
db = client.taobao
collection = db.products

result = collection.find()
if not os.path.exists('./img'):
    os.mkdir('./img')
for item in result:
    url = 'https:'+item['image']
    # img_file = requests.get(url=url).content
    # img_name = re.findall(r'.*/(.*\.jpg)',url)[0]
    img_name = './img/' + url.split('/')[-1]
    if os.path.isfile(img_name):
        continue
    img_bin = requests.get(url=url).content
    print(url + '开始下载')
    with open(img_name,'wb') as f:
        f.write(img_bin)
        print(url + '下载完成')

